import axios from 'axios'

const service = axios.create({
   baseURL: process.env.VUE_APP_BASE_API,
   timeout: 60000,
})

// 请求拦截
service.interceptors.request.use(
   config => {
      config.headers.Authorization = sessionStorage.getItem('access_token') || '';
      config.headers.clientType = 'WEB'
      config.headers.lang = localStorage.getItem('lang')
      return config
   },
   error => {
      return Promise.reject(error)
   }
)

// 响应拦截
service.interceptors.response.use(
   response => {
      let res = response.data
      return Promise.resolve(res)
   },
   error => {
      let err = error.response
      if (err.status === 401) getRefreshToken()
      return Promise.reject(err)
   }
)

export default service

